﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace ServeurRSS_1_0.Source.Model
{

    [DataContract]
    public class DataDirectory
    {
        [DataMember]
        public System.Guid id
        { get; set; }

        [DataMember]
        public System.Guid fk_id_Directories_User
        { get; set; }

        [DataMember]
        public string name
        { get; set; }

        [DataMember]
        public int type
        { get; set; }

        [DataMember]
        public System.Guid fk_id_Directories_Parent
        { get; set; }

    }

    public class DirectoryModel : ParsingModel
    {

        public DataDirectory getDataDirectory(DirectoriesUser userDir)
        {
            DataDirectory res = new DataDirectory();
            res.id = userDir.id;
            res.name = userDir.name;
            if (userDir.type != null)
                res.type = (int)userDir.type;
            res.type = 0;
            if (userDir.fk_id_Directories_User != null)
                res.fk_id_Directories_User = (System.Guid)userDir.fk_id_Directories_User;
            else
                res.fk_id_Directories_User = System.Guid.Empty;
            if (userDir.fk_id_Directories_Parent != null)
                res.fk_id_Directories_Parent = (System.Guid)userDir.fk_id_Directories_Parent;
            else
                res.fk_id_Directories_Parent = System.Guid.Empty;
            return res;
        }

        public DirectoriesUser getDirectoryUser(DataDirectory userDir)
        {
            DirectoriesUser res = new DirectoriesUser();
            res.id = userDir.id;
            res.name = userDir.name;
            res.type = userDir.type;
            if (userDir.fk_id_Directories_User.Equals(System.Guid.Empty))
                res.fk_id_Directories_User = null;
            else
                res.fk_id_Directories_User = userDir.fk_id_Directories_User;
            if (userDir.fk_id_Directories_Parent.Equals(System.Guid.Empty))
                res.fk_id_Directories_Parent = null;
            else
                res.fk_id_Directories_Parent = userDir.fk_id_Directories_Parent;
            return res;
        }

        public DirectoriesUser getRootDirectoryForUser(System.Guid idUser)
        {
            try
            {
                LinkServeurRSSDataContext db = new LinkServeurRSSDataContext();

                return db.DirectoriesUsers.FirstOrDefault(elem => elem.fk_id_Directories_User == idUser && elem.fk_id_Directories_Parent == null);
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public List<DataDirectory> getListDataDirectory(List<DirectoriesUser> list)
        {
            List<DataDirectory> res = new List<DataDirectory>();

            foreach (DirectoriesUser tmp in list)
                res.Add(this.getDataDirectory(tmp));
            return res;
        }

        public List<DirectoriesUser> getListUserDirectory(List<DataDirectory> list)
        {
            List<DirectoriesUser> res = new List<DirectoriesUser>();

            foreach (DataDirectory tmp in list)
                res.Add(this.getDirectoryUser(tmp));
            return res;
        }

        public List<DirectoriesUser> getListChildDirectory(System.Guid idDir)
        {
            LinkServeurRSSDataContext db = new LinkServeurRSSDataContext();
            return db.DirectoriesUsers.Where(p => p.fk_id_Directories_Parent == idDir).ToList();
        }

        //public Dictionary<string, RssTree> getRecursiveSubDirectories(List<DirectoriesUser> list)
        public virtual RssTree getRecursiveSubDirectories(RssTree parent)
        {

 //           Dictionary<string, RssTree> res = new Dictionary<string, RssTree>();
            List<DirectoriesUser> list = getListChildDirectory(parent.currentDir.id);
            try
            {
                LinkServeurRSSDataContext db = new LinkServeurRSSDataContext();
                foreach (DirectoriesUser tmp in list)
                {
                    RssTree node = new RssTree(getDataDirectory(tmp));
                    node.rssList = null;
                    //IQueryable<DirectoriesUser> dirList = from elem in db.DirectoriesUsers
                    //                                      where elem.fk_id_Directories_Parent == tmp.id
                    //                                      select elem;

                    node = this.getRecursiveSubDirectories(node);
                    parent.subDir.Add(tmp.name, node);
                }
                return parent;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public RssTree getAll(System.Guid idUser)
        {
            RssTree res = new RssTree(getDataDirectory(getRootDirectoryForUser(idUser)));
            res = this.getRecursiveSubDirectories(res);
            return res;
        }



        public string createDirectory(System.Guid userId, DataDirectory newDir, DataDirectory parent = null)
        {
            newDir.fk_id_Directories_User = userId;
            if (parent != null)
                newDir.fk_id_Directories_Parent = parent.id;
            else
                newDir.fk_id_Directories_Parent = System.Guid.Empty;

            try
            {
                LinkServeurRSSDataContext db = new LinkServeurRSSDataContext();
                db.DirectoriesUsers.InsertOnSubmit(getDirectoryUser(newDir));
                db.SubmitChanges();
                return "OK";
            }
            catch (Exception e)
            {
                return "NOK";
            }
        }

        public string createDirectory(System.Guid userId, string nameDir, DataDirectory parent = null)
        {
            DataDirectory newDir = new DataDirectory
            {
                id = Guid.NewGuid(),
                name = nameDir,
                type = 0
            };
            return createDirectory(userId, newDir, parent);
        }
    }
}